unction confusion_matrix = calc1(train_size=750)
% Calculates (and returns) the confusion matrix and prints the 
% error rate for the set contained in banana.mat 
% using a normally distributed bayes classifier.  
%
% By default the training sets contain 750 of the elements. 
%
% 2012 Maarten Inja & Chiel Kooijman. 

load banana.mat

% split the data into training sets and test sets
A_train = A(1:train_size, :);
A_test = A(1:(1000-train_size), :);
B_train = B(1:train_size, :);
B_test = B(1:(1000-train_size), :);

% calculate the covariance matrices for the training sets
cov_A = cov(A_train);
cov_B = cov(B_train);

% calculate the mean for the training sets
mean_A = mean(A_train);
mean_B = mean(B_train);

% probabilities for A classified using A
result_AA = mvnpdf(A_test, mean_A, cov_A);
% probabilities for A classified using B
result_AB = mvnpdf(A_test, mean_B, cov_B);
% probabilities for B classified using A
result_BA = mvnpdf(B_test, mean_A, cov_A);
% probabilities for B classified using B
result_BB = mvnpdf(B_test, mean_B, cov_B);

% The prior is the same for all classes (0.5),
% The evidence is the same for each x. 
% This is way we simplify the bayes rule: 
% we can just compare the likelihoods. 

% create confusion matrix
true_A = sum(result_AA > result_AB);
true_B = sum(result_BA < result_BB);
false_A = sum(result_AA < result_AB);
false_B = sum(result_BA > result_BB);
confusion_matrix = [true_A false_A; false_B, true_B];

% print error rate
error_rate = (false_A + false_B) / (true_A + true_B + false_A + false_B)
